# -*- coding: UTF-8 -*-
# @Time : 2022/1/4 0004 16:38
# @Author : lyw
# @Remark : 烟感
import logging

from mxsoftpy import DB
from mxsoftpy.exception import DBError

from db.public.IOTManager.iot_base import IOTBaseDB
from index import mini_cache, _
from utils.classes.custom_exception import VerifyError


class GASDeviceSelectDB(IOTBaseDB):
    device_type = "GasDetector"
    porperty_list = ["mxlabel", "mxdevicetype", "mxparent"]

    def param_middle(self, data_obj) -> dict:
        """
        基础查询 参数
        @param data_obj:
        @return:
        """
        return {"iot_code": data_obj.iot_code, "device_type": data_obj.device_type}


class GASDeviceListDB(GASDeviceSelectDB):
    def get_sub_monitor(self, code, device_data):
        pass


class GASDeviceRetrieveDB(IOTBaseDB):

    def select_sub_monitor(self, data_key):
        """
        查找设备的监测点
        @param data_key:
        @return:
        """
        try:
            res_monitor_list = []
            monitor_list_list = self.tree.open("MXSE", "1\\1.SD\\%s\\%s.SubMonitor" % (data_key, data_key),
                                               file="ccubase").sub_keys()
            for monitor_key in monitor_list_list:
                monitor_value = self.tree.open("MXSE",
                                               "1\\1.SD\\%s\\%s.SubMonitor\\%s" % (data_key, data_key, monitor_key),
                                               file="ccubase").items("mxpluginname")
                monitor_value["code"] = monitor_key
                res_monitor_list.append(monitor_value)
        except DBError as e:
            logging.error(e)
            raise VerifyError(_("监测点查询错误"))
        return res_monitor_list

    def read_template(self):
        """
        读取设备模板
        @return:
        """
        db_path = "EntityTemplate"
        ups_temp_dict = {}
        ups_temp_cache = mini_cache.get("GAS_DETECTOR_TEMP_CACHE")
        if ups_temp_cache:
            logging.info("GAS_DETECTOR 模板读取缓存")

            return mini_cache["GAS_DETECTOR_TEMP_CACHE"]
        for monitor_sub_code in self.tree.open(db_path, "IOT\\GasDetector", file="base").sub_keys():
            sub_path = "\\".join(["IOT", "GasDetector", monitor_sub_code, "MonitorEx"])
            for monitor_key in self.tree.open(db_path, sub_path, file="base").sub_keys():
                monitor_value = self.tree.open(db_path, "\\".join([sub_path, monitor_key]), file="base").items()

                self.query_temp(monitor_value["mxpluginname"], ups_temp_dict)

        mini_cache.set("GAS_DETECTOR_TEMP_CACHE", ups_temp_dict, 24 * 60 * 60)
        logging.info("GAS_DETECTOR 模板设置了缓存")
        return ups_temp_dict

    def query_temp(self, mxpluginname, plugin_list: dict):
        """
        获取 plugin 数据
        @param mxpluginname:
        @param plugin_list:
        @return:
        """
        path = "PluginData"
        try:
            for i in self.tree.open(path, mxpluginname + "\\Output", file="base").sub_keys():
                plugin_data = self.tree.open(path, mxpluginname + "\\Output\\%s" % i, file="base").items(
                    ["mxlabel", "mxunit"])
                plugin_data["key"] = i
                plugin_list[mxpluginname] = plugin_data
        except DBError as e:
            logging.error(e)
